/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年1月28日 下午12:00:53
 */
package com.lenovo.lmrp.server.assigntracer.spi;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.lenovo.lmrp.api.assigntrace.ITraceWorkflow;
import com.lenovo.lmrp.api.model.RestResponse;
import com.lenovo.lmrp.api.model.entity.ActivityFormPO;
import com.lenovo.lmrp.api.model.entity.ProcessPO;
import com.lenovo.lmrp.api.model.entity.TicketProcessPO;
import com.lenovo.lmrp.api.model.view.TicketProcessVO;
import com.lenovo.lmrp.api.model.view.WorkFlowSelector;
import com.lenovo.lmrp.api.model.view.WorkFlowVO;
import com.lenovo.lmrp.server.assigntracer.domain.workflow.ProcessServiceImpl;
import com.lenovo.lmrp.server.assigntracer.repository.WorkflowRepository;
import com.lenovo.sdc.framework.annotation.RestService;
import com.lenovo.sdc.framework.dao.ADaoFactory;
import com.lenovo.sdc.framework.dao.ICommonDAO;
import com.lenovo.sdc.framework.dao.IQueryExecutor;
import com.lenovo.sdc.framework.dao.RecordFilter;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;
/**
 * 
 * @author suo
 * @version $Id: TraceWorkflowImpl.java, v 0.1 2016年2月29日 上午9:46:27 suo Exp $
 */
@RestService
@Path("/assigntrace/v1")
@Produces("application/json;charset-utf-8")
@Consumes("application/json;charset-utf-8")
public class TraceWorkflowImpl implements ITraceWorkflow {

    private WorkflowRepository repository;
    
    @Inject
    public void set(WorkflowRepository repository) {
        this.repository = repository;
    }
    
    @Override
    public RestResponse<WorkFlowVO> getWorkFlow(String itsmcode) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public RestResponse<WorkFlowVO> importWorkFlow(String itsmCode, String bpmnfile) {
        ProcessServiceImpl impl = new ProcessServiceImpl();
        ProcessPO po = impl.deploayProcess(itsmCode, bpmnfile);

        repository.storeWorkflow(itsmCode, po);
        return new RestResponse<WorkFlowVO>(0, "OK", null);
    }

    @Override
    public void enableOrDisableWorkFlow(String itsmcode, String processid, int status) {
        // TODO Auto-generated method stub

    }
    /**
     * 获取工单对应工作流所有节点及当前节点
     * 
     * @author suo
     * @see com.lenovo.lmrp.api.assigntrace.ITraceWorkflow#getTicketProcessInstance(java.lang.String, java.lang.String)
     */
    @Override
    public RestResponse<TicketProcessVO> getTicketProcessVO(String itsmcode, String ticketid) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO()) {
            RecordFilter filter=new RecordFilter();
            //获取hql的queryExecutor
            dao.filteByCondition(TicketProcessVO.class, filter);
            IQueryExecutor hqlExecutor= dao.getRegisteredFinder(ICommonDAO.FinderEnum.HQL);
            //查询工单与流程对应关系
            String hql="from TicketProcessPO where ticketId = ? and status <> 2 ";
            TicketProcessPO tp= hqlExecutor.getSingleRecord(hql,TicketProcessPO.class,new Object[]{ticketid});
            //查询所有在app显示的节点
            hql="from ActivityFormPO where processId = ? and showForApp = 1 order by sortForApp asc";
            List<ActivityFormPO> activityFormList = hqlExecutor.findMappedRecord(hql,ActivityFormPO.class,new Object[]{tp.getProcessId()});
            //查询当前所在节点
            hql="from ActivityFormPO where processId = ? and showForApp = 1 and activityKey = ?";
            ActivityFormPO currentActivity=hqlExecutor.getSingleRecord(hql,ActivityFormPO.class,new Object[]{tp.getProcessId(),tp.getActivityKey()});
            //封装TicketProcessVO
            TicketProcessVO tpVO=new TicketProcessVO();
            tpVO.setCurrentActivity(currentActivity);
            tpVO.setActivityList(activityFormList);
            return new RestResponse<>(0, "OK", tpVO);
        }
        catch (Exception e) {
            throw new TraceableException(ErrorCode.DB_QUERY_FAILED, e);
        }
    }

    @Override
    public void createWorkFlowSelector(String itsmcode, String name, String conditions, String processid) {
        // TODO Auto-generated method stub

    }

    @Override
    public RestResponse<List<WorkFlowSelector>> getWorkFlowSeletor(String itsmcode) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void deleteWorkFlowSelector(String itsmcode, String selectorid) {
        // TODO Auto-generated method stub

    }

    @Override
    public void updateWorkFlowSelector(String itsmcode, String selectorid, WorkFlowSelector workFlowSelector) {
        // TODO Auto-generated method stub

    }

    @Override
    public RestResponse<ActivityFormPO> getWorkFlowActivity(String itsmcode, String activityid) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO()) {
            ActivityFormPO o = dao.get(ActivityFormPO.class, activityid);
            return new RestResponse<>(0, "OK", o);
        }
        catch (Exception e) {
            throw new TraceableException(ErrorCode.DB_QUERY_FAILED, e);
        }
    }

}
